import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

const router= new Router({
  routes: [
    {
      path: '/',
      redirect: '/login',
    },
    {
      path: '/login',
      component: resolve => require(['../components/Login.vue'], resolve)
    },
    {
      path: '/createorder',
      component: resolve => require(['../components/Create/CreateOrder.vue'], resolve),
      meta: {
        //requireAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
      }
    },
    {
      path: '/createspstorage',
      component: resolve => require(['../components/Create/CreateSpstorage.vue'], resolve),
      meta: {
        //requireAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
      }
    },
    {
      path: '/createsupplier',
      component: resolve => require(['../components/Create/CreateSupplier.vue'], resolve),
      meta: {
        //requireAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
      }
    },
    {
      path: '/createcpstorage',
      component: resolve => require(['../components/Create/CreateCpstorage.vue'], resolve),
      meta: {
        //requireAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
      }
    },
    {
      path: '/createcustomer',
      component: resolve => require(['../components/Create/CreateCustomer.vue'], resolve),
      meta: {
        //requireAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
      }
    },
    {
      path: '/print',
      component: resolve => require(['../components/Print.vue'], resolve),
      meta: {
        //requireAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
      }
    },
    {
      path: '/homepage',
      component: resolve => require(['../components/common/Menu.vue'], resolve),
      children:[
        {
          path: '/homepage',
          component: resolve => require(['../components/Homepage.vue'], resolve),
          meta: {
            //requireAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
          }
        },
        {
          path: '/suppliers',
          component: resolve => require(['../components/Suppliers.vue'], resolve),
          meta: {
            //requireAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
          }
        },
        {
          path: '/spstorage',
          component: resolve => require(['../components/Spstorage.vue'], resolve),
          meta: {
            //requireAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
          }
        },
        {
          path: '/customers',
          component: resolve => require(['../components/Customers.vue'], resolve),
          meta: {
            //requireAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
          }
        },
        {
          path: '/cpstorage',
          component: resolve => require(['../components/Cpstorage.vue'], resolve),
          meta: {
            //requireAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
          }
        },
        {
          path: '/order',
          component: resolve => require(['../components/Order.vue'], resolve),
          meta: {
            //requireAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
          }
        },
        {
          path: '/orderdetail',
          component: resolve => require(['../components/OrderDetail.vue'], resolve),
          meta: {
            //requireAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
          }
        },
        {
          path: '/users',
          component: resolve => require(['../components/Users.vue'], resolve),
          meta: {
            //requireAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
          }
        }
      ]
    }
  ]
})
router.beforeEach((to, from, next) => {
  if (to.meta.requireAuth) {  // 判断该路由是否需要登录权限
    if (localStorage.getItem("username")) {  //
      next();
    }
    else {
      next({
        path: '/login',
        query: {redirect: to.fullPath}  // 将跳转的路由path作为参数，登录成功后跳转到该路由
      })
    }
  }
  else {
    next();
  }
})
export default router
